package com.bto.solar.work.utils;

import com.alibaba.fastjson2.JSONArray;
import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizCode;

import java.util.ArrayList;
import java.util.List;

public class JsonToListUtils {

    //JSONArray转成List
    public static List<Long> convertJSONArrayToList(String jsonArrayStr) {
        List<Long> longList = new ArrayList<>();
        try {
            JSONArray jsonArray = JSONArray.parse(jsonArrayStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                longList.add(jsonArray.getLong(i));
            }
        } catch (NullPointerException e) {
            throw new BizException("jsonArrayStr为空");
        } catch (Exception e) {
            throw new BizException(BizCode.JSON_TO_LIST_ERROR);
        }
        return longList;
    }


    //JSONArray转成List
    public static List<String> convertJSONArrayToStringList(String jsonArrayStr) {
        List<String> longList = new ArrayList<>();
        try {
            JSONArray jsonArray = JSONArray.parse(jsonArrayStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                longList.add(jsonArray.getString(i));
            }
        } catch (NullPointerException e) {
            throw new BizException("jsonArrayStr为空");
        } catch (Exception e) {
            throw new BizException(BizCode.JSON_TO_LIST_ERROR);
        }
        return longList;
    }
}
